package tinycloud.gui.frame;

import tinycloud.gui.action.QuitAction;
import tinycloud.gui.action.PreferencesAction;
import tinycloud.gui.action.AboutAction;
import javax.swing.*;
import tinycloud.Daemon;

/**
 * Fenêtre principale de l'application.
 * @author Nicolas CHARREL
 */
public class MainFrame extends JFrame {

    private JProgressBar progressBar;
    public MainFrame() {
        super();
        Daemon daemon = new Daemon(this);
        setTitle("Tiny Cloud");
        createMenuBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        setResizable(false);
        JPanel pane = new JPanel();
        pane.add(new JLabel("Tiny cloud initialisé.", JLabel.CENTER));
        setContentPane(pane);
        pack();
        setVisible(true);
        progressBar = null;
    }

    private void createMenuBar() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = null;
        JMenuItem menuItem = null;

        // Menu fichier
        menu = new JMenu("Fichier");
        menuItem = new JMenuItem("Quitter");
        menuItem.addActionListener(new QuitAction(this));
        menu.add(menuItem);
        menuBar.add(menu);

        // Menu édition
        menu = new JMenu("Edition");
        menuItem = new JMenuItem("Préférences");
        menuItem.addActionListener(new PreferencesAction());
        menu.add(menuItem);
        menuBar.add(menu);

        // Menu aide
        menu = new JMenu("Aide");
        menuItem = new JMenuItem("A propos");
        menuItem.addActionListener(new AboutAction());
        menu.add(menuItem);
        menuBar.add(menu);
        
        setJMenuBar(menuBar);
    }

    public void setError(String error) {
        setContentPane(new JLabel(error));
        pack();
        progressBar = null;
        this.validate();
    }

    public void setProgressBar(int i, int j) {
        if(progressBar == null) {
            progressBar = new JProgressBar();
            progressBar.setStringPainted(true);
            setContentPane(progressBar);
        }
        progressBar.setValue((int)((float)i/j*100));
        this.validate();
    }

    public void setEnd() {
        setContentPane(new JLabel("Fin de la mise a jour"));
        pack();
        progressBar = null;
        this.validate();
    }
}
